package com.example.skyline.controller;

import com.example.skyline.Utils.Page;
import com.example.skyline.common.Result;
import com.example.skyline.entity.Query;
import com.example.skyline.entity.User;
import com.example.skyline.service.UserManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/UserManagement")
@CrossOrigin
public class UserManagementController {
    @Autowired
    private UserManagementService userManagementService;


    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        userManagementService.addUser(user);
        Map<String, Object> map = new HashMap<>();
        if (user!= null){
            map.put("status", 200);
            map.put("message", "添加用户成功");
            }
        else {
            map.put("status", 444);
            map.put("message", "添加用户失败");
        }
        return Result.success(map);
    }


    @DeleteMapping("/deleteUser/{userId}")
    public Result deleteUser(@PathVariable("userId") Integer userId) {
        userManagementService.deleteUser(userId);
        Map<String, Object> map = new HashMap<>();
        if (userId != null){
            map.put("status", 200);
            map.put("message", "删除用户成功");
            }
        else {
            map.put("status", 444);
            map.put("message", "删除用户失败");
        }
        return Result.success(map);
    }

    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        userManagementService.updateUser(user);
        Map<String, Object> map = new HashMap<>();
        if (user != null){
            map.put("status", 200);
            map.put("message", "更新用户成功");
            }
        else {
            map.put("status", 444);
            map.put("message", "更新用户失败");
        }
        return Result.success(map);
    }

    @GetMapping("/getUser/{userId}")
    public Result getUser(@PathVariable("userId") Integer userId) {
        User user = userManagementService.getUserById(userId);
        Map<String, Object> map = new HashMap<>();
        if (user != null){
            map.put("status", 200);
            map.put("data", user);
            }
        else {
            map.put("status", 444);
            map.put("message", "获取用户失败");
        }
        return Result.success(map);
    }

    //查询所有用户（模糊查询 分页）
    @GetMapping("/getAllUser")
    public Result getAllUser(Query query){
        // 解析前端传递的参数（Query类需包含pageNum、pageSize、userName、name等字段）
        Page page = new Page(query.getPageNum(), query.getPageSize(),
                userManagementService.countAll(query.getUserName(), query.getName()));
        page.setList(userManagementService.selectAll(
                query.getUserName(),
                query.getName(),
                query.getPageNum(),
                query.getPageSize()
        ));
        Map<String, Object> map = new HashMap<>();
        map.put("list", page.getList());
        map.put("count", page.getTotalCount());
        map.put("pageNum", query.getPageNum());
        return Result.success(map);
    }

}


